package AMS.model;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ActTable{ 
	public void save(String stufrom, String stuto, String obj, String classname,String relation){	
		Activity act = new Activity();   
		
		act.setTime (new Timestamp(System.currentTimeMillis()));
		act.setStufrom(stufrom);   
		act.setStuto(stuto);  
		act.setObj(obj); 
		act.setClassname(classname);
		act.setRelation(relation);
       
		Session session = HibernateUtil.getSessionFactory().openSession();           
        Transaction tx= session.beginTransaction();         
        session.save(act);
        tx.commit();         
        session.close();                             
        HibernateUtil.shutdown();             
    }
    public ArrayList<String> selectfromobj(String stufrom){ 	      // update :order by act.time desc
	    Session session = HibernateUtil.getSessionFactory().openSession(); 
	    Query query = session.createQuery("select act.obj from Activity as act where act.stufrom = ? order by act.time desc");
	
	    query.setString(0, stufrom);
	  	  
	    List result = query.list();	
	     
	    Iterator it = result.iterator();
	     
	    ArrayList<String> obj = new ArrayList<String>();
	     
	    while(it.hasNext()){
	       obj.add((String)it.next());
	    }        
        return obj; 
    }
    public ArrayList<Timestamp> selectfromtime(String stufrom){ 	      // update :order by act.time desc
	    Session session = HibernateUtil.getSessionFactory().openSession(); 
	    Query query = session.createQuery("select act.time from Activity as act where act.stufrom = ? order by act.time desc");
	
	    query.setString(0, stufrom);
	  	  
	    List result = query.list();	
	     
	    Iterator it = result.iterator();
	     
	    ArrayList<Timestamp> time = new ArrayList<Timestamp>();
	     
	    while(it.hasNext()){
	       time.add((Timestamp)it.next());
	    }        
        return time; 
    }
    public ArrayList<String> selectfromclassname(String stufrom){ 	      // update :order by act.time desc
	    Session session = HibernateUtil.getSessionFactory().openSession(); 
	    Query query = session.createQuery("select act.classname from Activity as act where act.stufrom = ? order by act.time desc");
	
	    query.setString(0, stufrom);
	  	  
	    List result = query.list();	
	     
	    Iterator it = result.iterator();
	     
	    ArrayList<String> classname = new ArrayList<String>();
	     
	    while(it.hasNext()){
	       classname.add((String)it.next());
	    }        
        return classname; 
    }
    public ArrayList<String> selectfromrelation(String stufrom){ 	     // update :order by act.time desc 
	    Session session = HibernateUtil.getSessionFactory().openSession(); 
	    Query query = session.createQuery("select act.relation from Activity as act where act.stufrom = ? order by act.time desc");
	
	    query.setString(0, stufrom);
	  	  
	    List result = query.list();	
	     
	    Iterator it = result.iterator();
	     
	    ArrayList<String> relation = new ArrayList<String>();
	     
	    while(it.hasNext()){
	       relation.add((String)it.next());
	    }        
        return relation; 
    }
    public ArrayList<String> selectfromstuto(String stufrom){ 	      // by asun
	    Session session = HibernateUtil.getSessionFactory().openSession(); 
	    Query query = session.createQuery("select act.stuto from Activity as act where act.stufrom = ? order by act.time desc");
	
	    query.setString(0, stufrom);
	  	  
	    List result = query.list();	
	     
	    Iterator it = result.iterator();
	     
	    ArrayList<String> stuto = new ArrayList<String>();
	     
	    while(it.hasNext()){
	    	stuto.add((String)it.next());
	    }        
        return stuto; 
    }
    public ArrayList<String> selecttoobj(String stuto){ 	      // update :order by act.time desc
	     Session session = HibernateUtil.getSessionFactory().openSession(); 
	     Query query = session.createQuery("select act.obj from Activity as act where act.stuto = ? order by act.time desc");
	
	     query.setString(0, stuto);
	  	  
	     List result = query.list();	
	     
	     Iterator it = result.iterator();
	     
	     ArrayList<String> obj = new ArrayList<String>();
	     
	     while(it.hasNext()){
	    	 obj.add((String)it.next());
	     }        
       return obj; 
    }
    public ArrayList<Timestamp> selecttotime(String stuto){ 	      // update :order by act.time desc
	     Session session = HibernateUtil.getSessionFactory().openSession(); 
	     Query query = session.createQuery("select act.time from Activity as act where act.stuto = ? order by act.time desc");
	
	     query.setString(0, stuto);
	  	  
	     List result = query.list();	
	     
	     Iterator it = result.iterator();
	     
	     ArrayList<Timestamp> time = new ArrayList<Timestamp>();
	     
	     while(it.hasNext()){
	    	 time.add((Timestamp) it.next());
	     }        
         return time; 
    }
    public ArrayList<String> selecttoclassname(String stuto){ 	      // update :order by act.time desc
	     Session session = HibernateUtil.getSessionFactory().openSession(); 
	     Query query = session.createQuery("select act.classname from Activity as act where act.stuto = ? order by act.time desc");
	
	     query.setString(0, stuto);
	  	  
	     List result = query.list();	
	     
	     Iterator it = result.iterator();
	     
	     ArrayList<String> classname = new ArrayList<String>();
	     
	     while(it.hasNext()){
	    	 classname.add((String)it.next());
	     }        
         return classname; 
    }
    public ArrayList<String> selecttorelation(String stuto){ 	      // update :order by act.time desc
	     Session session = HibernateUtil.getSessionFactory().openSession(); 
	     Query query = session.createQuery("select act.relation from Activity as act where act.stuto = ? order by act.time desc");
	
	     query.setString(0, stuto);
	  	  
	     List result = query.list();	
	     
	     Iterator it = result.iterator();
	     
	     ArrayList<String> relation = new ArrayList<String>();
	     
	     while(it.hasNext()){
	    	 relation.add((String)it.next());
	     }        
      return relation; 
   }
    public ArrayList<String> selecttostufrom(String stuto){ 	// by asun      
	     Session session = HibernateUtil.getSessionFactory().openSession(); 
	     Query query = session.createQuery("select act.stufrom from Activity as act where act.stuto = ? order by act.time desc");
	
	     query.setString(0, stuto);
	  	  
	     List result = query.list();	
	     
	     Iterator it = result.iterator();
	     
	     ArrayList<String> stufrom = new ArrayList<String>();
	     
	     while(it.hasNext()){
	    	 stufrom.add((String)it.next());
	     }        
     return stufrom; 
  }
}